package edu.nyu.msis.utilities;

public class ManagedBuffer {
    char[] readBuffer;
    int pos;

    public ManagedBuffer() {
        readBuffer = new char[20];
        pos = -1;
    }
    
    public ManagedBuffer(int size) {
    	readBuffer = new char[size];
    	pos = -1;
    }

    private void reset() {
        pos = -1;
    }

    public void addCharToBuffer(Character readChar) {
        pos++;
        readBuffer[pos] = readChar;
    }

    public Integer convertBuffToInt() {
        Integer i = 0;
        int exp = 1;
        if (pos<0) {
        	return -1;
        }
        for (int j=pos; j>=0; j--) {
            i += exp*(readBuffer[j]-'0');
            exp *= 10;
        }
        reset(); //Reset the buffer since you have returned the value
        return i;
    }

    public String convertBuffToString() {
        String s;
        char[] temp = new char[pos+1];
        if (pos<0) {
        	return null;
        }
        for (int i=0; i<=pos; i++) {
            temp[i] = readBuffer[i];
        }
        s = new String(temp);
        reset(); //Reset the buffer since you have returned the value
        return s;
    }
}
